import { Controller, Post, Body, Get, Param } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { CustomerServiceService } from './customer-service.service';
import { CreateSessionDto } from './dto/create-session.dto';
import { CreateMessageDto } from './dto/create-message.dto';
import { AssignAgentDto } from './dto/assign-agent.dto';
import { CustomerServiceGroup } from './entities/group.entity';
import { CustomerServiceAgent } from './entities/agent.entity';

@ApiTags('客服系统')
@Controller('customer-service')
export class CustomerServiceController {
  constructor(private readonly csService: CustomerServiceService) {}

  @Post('session')
  @ApiOperation({ summary: '创建会话' })
  createSession(@Body() dto: CreateSessionDto) {
    return this.csService.createSession(dto);
  }

  @Post('session/assign')
  @ApiOperation({ summary: '分配客服' })
  assignAgent(@Body() dto: AssignAgentDto) {
    return this.csService.assignAgent(dto);
  }

  @Post('message')
  @ApiOperation({ summary: '发送消息' })
  sendMessage(@Body() dto: CreateMessageDto) {
    return this.csService.sendMessage(dto);
  }

  @Get('session/:sessionId/messages')
  @ApiOperation({ summary: '获取会话消息' })
  getSessionMessages(@Param('sessionId') sessionId: number) {
    return this.csService.getSessionMessages(sessionId);
  }

  @Get('user/:userId/sessions')
  @ApiOperation({ summary: '获取用户所有会话' })
  getUserSessions(@Param('userId') userId: number) {
    return this.csService.getUserSessions(userId);
  }

  @Get('agent/:agentId/sessions')
  @ApiOperation({ summary: '获取客服所有会话' })
  getAgentSessions(@Param('agentId') agentId: number) {
    return this.csService.getAgentSessions(agentId);
  }

  @Post('group')
  @ApiOperation({ summary: '创建客服分组' })
  createGroup(@Body() body: { name: string; description?: string }) {
    return this.csService['groupRepo'].save(body);
  }

  @Get('group')
  @ApiOperation({ summary: '获取所有客服分组' })
  getGroups() {
    return this.csService['groupRepo'].find();
  }

  @Post('agent')
  @ApiOperation({ summary: '创建客服账号' })
  async createAgent(@Body() body: { username: string; name: string; groupId?: number; role?: 'agent' | 'supervisor'; maxConcurrentSessions?: number }) {
    const agent: any = {
      username: body.username,
      name: body.name,
      role: body.role || 'agent',
      maxConcurrentSessions: body.maxConcurrentSessions || 5,
    };
    if (body.groupId) agent.group = { id: body.groupId };
    return this.csService['agentRepo'].save(agent);
  }

  @Get('agent')
  @ApiOperation({ summary: '获取所有客服账号' })
  getAgents() {
    return this.csService['agentRepo'].find({ relations: ['group'] });
  }

  @Post('session/:sessionId/close')
  @ApiOperation({ summary: '关闭会话' })
  closeSession(@Param('sessionId') sessionId: number, @Body() body: { reason?: string }) {
    return this.csService.closeSession(sessionId, body.reason);
  }

  @Post('session/:sessionId/transfer')
  @ApiOperation({ summary: '转接会话' })
  transferSession(@Param('sessionId') sessionId: number, @Body() body: { toAgentId: number }) {
    return this.csService.transferSession(sessionId, body.toAgentId);
  }

  @Post('session/:sessionId/rate')
  @ApiOperation({ summary: '评价会话' })
  rateSession(@Param('sessionId') sessionId: number, @Body() body: { rating: number; comment?: string }) {
    return this.csService.rateSession(sessionId, body.rating, body.comment);
  }

  @Get('agent/:agentId/stats')
  @ApiOperation({ summary: '客服统计' })
  getAgentStats(@Param('agentId') agentId: number) {
    return this.csService.getAgentStats(agentId);
  }

  @Post('agent/:agentId/online')
  @ApiOperation({ summary: '客服上线/下线' })
  setAgentOnline(@Param('agentId') agentId: number, @Body() body: { online: boolean }) {
    return this.csService.setAgentOnline(agentId, body.online);
  }

  @Post('message/:messageId/read')
  @ApiOperation({ summary: '标记消息为已读' })
  markMessageRead(@Param('messageId') messageId: number) {
    return this.csService.markMessageRead(messageId);
  }

  @Get('session/:sessionId/unread')
  @ApiOperation({ summary: '获取会话未读消息数' })
  getSessionUnreadCount(@Param('sessionId') sessionId: number, @Body() body: { forAgent: boolean }) {
    return this.csService.getSessionUnreadCount(sessionId, body.forAgent);
  }

  @Get('user/:userId/unread')
  @ApiOperation({ summary: '获取用户所有未读消息数' })
  getUserUnreadCount(@Param('userId') userId: number) {
    return this.csService.getUserUnreadCount(userId);
  }

  @Get('agent/:agentId/unread')
  @ApiOperation({ summary: '获取客服所有未读消息数' })
  getAgentUnreadCount(@Param('agentId') agentId: number) {
    return this.csService.getAgentUnreadCount(agentId);
  }

  @Get('rank/session')
  @ApiOperation({ summary: '客服会话数排行' })
  getAgentSessionRank() {
    return this.csService.getAgentSessionRank();
  }

  @Get('rank/message')
  @ApiOperation({ summary: '客服消息数排行' })
  getAgentMessageRank() {
    return this.csService.getAgentMessageRank();
  }

  @Get('rank/rating')
  @ApiOperation({ summary: '客服平均评分排行' })
  getAgentRatingRank() {
    return this.csService.getAgentRatingRank();
  }
} 